package com.lfs.admin.service.impl;

import com.lfs.admin.dao.ChannelInfoDao;
import com.lfs.admin.model.entity.channel.ChannelInfoEntity;
import com.lfs.admin.model.entity.channel.ChannelToGroupEntity;
import com.lfs.admin.model.entity.channel.ChannelToProductEntity;
import com.lfs.admin.model.vo.channel.ChannelGroupVO;
import com.lfs.admin.model.vo.channel.ChannelToGroupVO;
import com.lfs.admin.model.vo.channel.ChannelToProductVO;
import com.lfs.admin.model.vo.channel.ChannelVO;
import com.lfs.admin.service.ChannelInfoService;
import com.lfs.base.exception.BusinessException;
import com.lfs.common.constant.CommonConstants;
import com.lfs.dao.service.SystemService;
import com.lfs.interfaces.log.service.LogFileService;
import com.lfs.interfaces.model.vo.LogFileVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChannelInfoServiceImpl implements ChannelInfoService {

    @Autowired
    private ChannelInfoDao channelInfoDao;

    @Autowired
    private LogFileService logFileService;

    private Logger logger = LoggerFactory.getLogger(ChannelInfoServiceImpl.class);

    @Override
    public List<ChannelInfoEntity> querySelectList(ChannelVO channelInfoVO) {
        return channelInfoDao.querySelectList(channelInfoVO);
    }

    @Override
    public List<ChannelInfoEntity> queryChannelInfoList(ChannelVO channelInfoVO) {
        List<ChannelInfoEntity> channelList = new ArrayList<>();
        try {
            channelList = channelInfoDao.queryChannelInfoList(channelInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return channelList;
    }

    @Override
    public ChannelInfoEntity getChannelById(Integer id) {
        return channelInfoDao.getChannelById(id);
    }

    @Override
    public List<ChannelToProductEntity> getChannelToProduct(ChannelToProductVO channelToProductVO) {
        return channelInfoDao.getChannelToProduct(channelToProductVO);
    }

    @Override
    public int updateChannelInfo(ChannelVO channelInfoVO) {
        return channelInfoDao.updateChannelInfo(channelInfoVO);
    }

    @Override
    public int updateChannelStatus(ChannelVO channelInfoVO) {
        return channelInfoDao.updateChannelStatus(channelInfoVO);
    }

    @Override
    public int updateChannelProductState(ChannelToProductVO channelToProductVO) {
        return channelInfoDao.updateChannelProductState(channelToProductVO);
    }

    @Override
    public int addChannelToProduct(ChannelToProductVO channelToProductVO) {
        int result = 0;
        List<ChannelToProductVO> channelToProductList = new ArrayList<>();
        try {
            channelToProductVO.getProductNums().forEach(productNum -> {
                ChannelToProductEntity productToGroup = channelInfoDao.getChannelToProductByCode(productNum, channelToProductVO.getChannelNum());
                if (null != productToGroup) {
                    logger.warn("该产品编码={}已存在于通道组中,请勿重复添加!", productNum);
                } else {
                    ChannelToProductVO addVo = new ChannelToProductVO();
                    addVo.setProductNum(productNum);
                    addVo.setChannelNum(channelToProductVO.getChannelNum());
                    addVo.setEndPrice(new BigDecimal(0.00));
                    addVo.setChannelPrice(new BigDecimal(0.00));
                    channelToProductList.add(addVo);
                }
            });
            result = channelInfoDao.addProductToChannel(channelToProductList);
            if (result > 0) {
                String logRemark = "通道关联产品编码为" + channelToProductVO.getProductNum() + "-" + channelToProductVO.getProductNums();
                String loginName = SystemService.getCurrentUser().getLoginName() == null ? "error" : SystemService.getCurrentUser().getLoginName();
                LogFileVO logFile = new LogFileVO();
                logFile.setLogType(CommonConstants.LOG_ADD_TYPE);
                logFile.setModifyType(CommonConstants.LOG_CHANNEL_GROUP_TYPE);
                logFile.setIpAddr(this.getClass().getName());
                logFile.setLogDetail(logRemark);
                logFile.setLogRemark(logRemark);
                logFile.setLoginName(loginName);
                logFile.setAfterUpdate("");
                logFile.setBeforeUpdate("");
                logFileService.insertLogFile(logFile);
                return result;
            } else {
                throw new BusinessException("添加产品信息失败");
            }
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public int addChannelInfo(ChannelVO channelInfoVO) {
        return channelInfoDao.addChannelInfo(channelInfoVO);
    }

    @Override
    public int deleteChannelInfo(Integer[] ids) {
        return channelInfoDao.deleteChannelInfo(ids);
    }

    @Override
    public List<ChannelInfoEntity> getChannelList() {
        return channelInfoDao.getChannelList();
    }

    @Override
    public List<ChannelInfoEntity> getChannelToGroupList(Integer channelNum) {
        return channelInfoDao.getChannelToGroupList(channelNum);
    }

}
